package com.example.springboot.utils;

import com.example.springboot.entity.Post;
import com.example.springboot.entity.Product;
import com.example.springboot.entity.User;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Component
public class GenerateDSL {
    private Map<Integer, String> orderMap = Map.of(0, "综合排序", 1, "published_time", 2, "likes", 3, "collect_count", 4, "views");
    private Map<Integer, String> userOrderMap = Map.of(0, "综合排序", 1, "fens-num", 2, "article-num");

    public String generateSearchQuery(String searchContent, Integer categoryId, Integer articleType, Integer orderType, Integer offset, Integer pageSize) {
        String order = orderMap.get(orderType);
        String res = String.format("""
                {
                  "from": "%d",
                  "size": "%d",
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "bool": {
                            "should": [
                              {
                                "match": {
                                  "summary": "%s"
                                }
                              },
                              {
                                "match": {
                                  "title": "%s"
                                }
                              }
                            ]
                          }
                        },
                        {
                          "term": {
                            "category_id": "%d"
                          }
                        },
                        {
                          "term": {
                            "article_type": "%d"
                          }
                        }
                      ]
                    }
                  },
                  "highlight": {
                    "fields": {
                      "summary": {
                        "require_field_match": "false",
                        "pre_tags": "<gaoliang>",
                        "post_tags": "</gaoliang>"
                      },
                      "title": {
                        "require_field_match": "false",
                        "pre_tags": "<gaoliang>",
                        "post_tags": "</gaoliang>"
                      }
                    }
                  },
                  "sort": [
                    {
                      "%s": {
                        "order": "desc"
                      }
                    }
                  ]
                }
                """, offset, pageSize, searchContent, searchContent, categoryId, articleType, order);
        if (order.equals("综合排序")) {
            res = res.replace("""
                    ,
                      "sort": [
                        {
                          "综合排序": {
                            "order": "desc"
                          }
                        }
                      ]
                                        """, "");
        }
        return res;
    }

    public String generateSearchQuery(Integer orderType, Integer offset, Integer pageSize) {
        String order = orderMap.get(orderType);
        String res = String.format("""
                {
                  "from": "%d",
                  "size": "%d",
                  "query": {
                    "match_all": {}
                  },
                  "sort": [
                    {
                      "%s": {
                        "order": "desc"
                      }
                    }
                  ]
                }
                """, offset, pageSize, order);
        if (order.equals("综合排序")) {
            res = res.replace("""
                    ,
                      "sort": [
                        {
                          "综合排序": {
                            "order": "desc"
                          }
                        }
                      ]
                                        """, "");
        }
        return res;
    }

    public String generateSearchQuery(String searchContent, Integer orderType, Integer offset, Integer pageSize) {
        String order = orderMap.get(orderType);
        String res = String.format("""
                {
                  "from": "%d",
                  "size": "%d",
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "bool": {
                            "should": [
                              {
                                "match": {
                                  "summary": "%s"
                                }
                              },
                              {
                                "match": {
                                  "title": "%s"
                                }
                              }
                            ]
                          }
                        }
                      ]
                    }
                  },
                  "highlight": {
                    "fields": {
                      "summary": {
                        "require_field_match": "false",
                        "pre_tags": "<gaoliang>",
                        "post_tags": "</gaoliang>"
                      },
                      "title": {
                        "require_field_match": "false",
                        "pre_tags": "<gaoliang>",
                        "post_tags": "</gaoliang>"
                      }
                    }
                  },
                  "sort": [
                    {
                      "%s": {
                        "order": "desc"
                      }
                    }
                  ]
                }
                """, offset, pageSize, searchContent, searchContent, order);
        if (order.equals("综合排序")) {
            res = res.replace("""
                    ,
                      "sort": [
                        {
                          "综合排序": {
                            "order": "desc"
                          }
                        }
                      ]
                                        """, "");
        }
        return res;
    }

    public String generateSearchQuery1(Integer categoryId, Integer orderType, Integer offset, Integer pageSize) {
        String order = orderMap.get(orderType);
        String res = String.format("""
                {
                  "from": "%d",
                  "size": "%d",
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "category_id": "%d"
                          }
                        }
                      ]
                    }
                  },
                  "sort": [
                    {
                      "%s": {
                        "order": "desc"
                      }
                    }
                  ]
                }
                """, offset, pageSize, categoryId, order);
        if (order.equals("综合排序")) {
            res = res.replace("""
                    ,
                      "sort": [
                        {
                          "综合排序": {
                            "order": "desc"
                          }
                        }
                      ]
                                        """, "");
        }
        return res;
    }

    public String generateSearchQuery2(Integer articleType, Integer orderType, Integer offset, Integer pageSize) {
        String order = orderMap.get(orderType);
        String res = String.format("""
                {
                  "from": "%d",
                  "size": "%d",
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "article_type": "%d"
                          }
                        }
                      ]
                    }
                  },
                  "sort": [
                    {
                      "%s": {
                        "order": "desc"
                      }
                    }
                  ]
                }
                """, offset, pageSize, articleType, order);
        if (order.equals("综合排序")) {
            res = res.replace("""
                    ,
                      "sort": [
                        {
                          "综合排序": {
                            "order": "desc"
                          }
                        }
                      ]
                                        """, "");
        }
        return res;
    }

    public String generateSearchQuery(Integer categoryId, Integer articleType, Integer orderType, Integer offset, Integer pageSize) {
        String order = orderMap.get(orderType);
        String res = String.format("""
                {
                  "from": "%d",
                  "size": "%d",
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "category_id": "%d"
                          }
                        },
                        {
                          "term": {
                            "article_type": "%d"
                          }
                        }
                      ]
                    }
                  },
                  "sort": [
                    {
                      "%s": {
                        "order": "desc"
                      }
                    }
                  ]
                }
                """, offset, pageSize, categoryId, articleType, order);
        if (order.equals("综合排序")) {
            res = res.replace("""
                    ,
                      "sort": [
                        {
                          "综合排序": {
                            "order": "desc"
                          }
                        }
                      ]
                                        """, "");
        }
        return res;
    }

    public String generateSearchQuery1(String searchContent, Integer articleType, Integer orderType, Integer offset, Integer pageSize) {
        String order = orderMap.get(orderType);
        String res = String.format("""
                {
                  "from": "%d",
                  "size": "%d",
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "bool": {
                            "should": [
                              {
                                "match": {
                                  "summary": "%s"
                                }
                              },
                              {
                                "match": {
                                  "title": "%s"
                                }
                              }
                            ]
                          }
                        },
                        {
                          "term": {
                            "article_type": "%d"
                          }
                        }
                      ]
                    }
                  },
                  "highlight": {
                    "fields": {
                      "summary": {
                        "require_field_match": "false",
                        "pre_tags": "<gaoliang>",
                        "post_tags": "</gaoliang>"
                      },
                      "title": {
                        "require_field_match": "false",
                        "pre_tags": "<gaoliang>",
                        "post_tags": "</gaoliang>"
                      }
                    }
                  },
                  "sort": [
                    {
                      "%s": {
                        "order": "desc"
                      }
                    }
                  ]
                }
                """, offset, pageSize, searchContent, searchContent, articleType, order);
        if (order.equals("综合排序")) {
            res = res.replace("""
                    ,
                      "sort": [
                        {
                          "综合排序": {
                            "order": "desc"
                          }
                        }
                      ]
                                        """, "");
        }
        return res;
    }

    public String generateSearchQuery2(String searchContent, Integer categoryId, Integer orderType, Integer offset, Integer pageSize) {
        String order = orderMap.get(orderType);
        String res = String.format("""
                {
                  "from": "%d",
                  "size": "%d",
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "bool": {
                            "should": [
                              {
                                "match": {
                                  "summary": "%s"
                                }
                              },
                              {
                                "match": {
                                  "title": "%s"
                                }
                              }
                            ]
                          }
                        },
                        {
                          "term": {
                            "category_id": "%d"
                          }
                        }
                      ]
                    }
                  },
                  "highlight": {
                    "fields": {
                      "summary": {
                        "require_field_match": "false",
                        "pre_tags": "<gaoliang>",
                        "post_tags": "</gaoliang>"
                      },
                      "title": {
                        "require_field_match": "false",
                        "pre_tags": "<gaoliang>",
                        "post_tags": "</gaoliang>"
                      }
                    }
                  },
                  "sort": [
                    {
                      "%s": {
                        "order": "desc"
                      }
                    }
                  ]
                }
                """, offset, pageSize, searchContent, searchContent, categoryId, order);
        if (order.equals("综合排序")) {
            res = res.replace("""
                    ,
                      "sort": [
                        {
                          "综合排序": {
                            "order": "desc"
                          }
                        }
                      ]
                                        """, "");
        }
        return res;
    }

    public String inseartSearchSuggest(String searchContent, String documentId) {
        String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        String res = String.format("""
                {
                  "id": "%s",
                  "search_suggest": "%s",
                  "last_time": "%s",
                  "search_count": 1
                }
                """, documentId, searchContent, currentTime);
        return res;
    }

    public String updateSearchSuggest() {
        String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        String res = String.format("""
                {
                  "script": {
                    "source": "ctx._source.search_count += 1; ctx._source.last_time = '%s';",
                    "lang": "painless"
                  }
                }
                """, currentTime);
        return res;
    }

    public String generateSearchSuggest(String searchContent) {
        String res = String.format("""
                {
                  "suggest": {
                    "searchSuggest": {
                      "text": "%s",
                      "completion": {
                        "field": "search_suggest.suggest",
                        "skip_duplicates": true,
                        "size": 10
                      }
                    }
                  }
                }
                """, searchContent);
        return res;
    }

    public String insertUser(User user) {
        String res = String.format("""
                        {
                            "id": "%s",
                            "username": "%s",
                            "nickname": "%s",
                            "email": "%s",
                            "phone": "%s",
                            "create_time": "%s",
                            "update_time": "%s",
                            "avatar": "%s",
                            "all_views": %d,
                            "article_num": %d,
                            "fens_num": %d,
                            "ip_address": "%s",
                            "signature": "%s",
                            "birthday": "%s"
                        }
                        """,
                user.getId(),
                user.getUsername(),
                user.getNickname(),
                user.getEmail(),
                user.getPhone(),
                user.getCreateTime(),
                user.getUpdateTime(),
                user.getAvatar(),
                user.getAllViews(),
                user.getArticleNum(),
                user.getFensNum(),
                user.getIpAddress(),
                user.getSignature(),
                user.getBirthday()
        );
        return res;
    }

    public String genareteSearchUserQuery(String searchContent, Integer offset, Integer pageSize) {
        String res = String.format("""
                                {
                  "from": "%d",
                  "size": "%d",
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "nickname": "%s"
                          }
                        }
                      ]
                    }
                  }
                }
                """, offset, pageSize, searchContent);
        return res;
    }

    public String genareteSearchUserQuery(String searchContent, Integer orderType, int offset, int pageSize) {
        String order = userOrderMap.get(orderType);
        String res = String.format("""
                                {
                  "from": "%d",
                  "size": "%d",
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "nickname": "%s"
                          }
                        }
                      ]
                    }
                  },
                  "sort": [
                    {
                      "%s": {
                        "order": "desc"
                      }
                    }
                  ]
                }
                """, offset, pageSize, searchContent, order);
        if (order.equals("综合排序")) {
            res = res.replace("""
                    ,
                      "sort": [
                        {
                          "综合排序": {
                            "order": "desc"
                          }
                        }
                      ]
                                        """, "");
        }
        return res;
    }

    public String genareteRelatedArticlesQuery(Post originalArticle) {
        String res = String.format("""
                        {
                          "from": 0,
                          "size": 8,
                          "query": {
                            "bool": {
                              "should": [
                                {
                                  "match": {
                                    "title": {
                                      "query": "%s",
                                      "boost": 0.75
                                    }
                                  }
                                },
                                {
                                  "match": {
                                    "tags": {
                                      "query": "%s",
                                      "boost": 0.25
                                    }
                                  }
                                }
                              ]
                            }
                          }
                        }
                """, originalArticle.getTitle(), originalArticle.getTags());
        return res;
    }

    public String insertProduct(Product product) {
        String res = String.format("""
                          {
                            "id": "%s",
                            "title": "%s",
                            "pic_path": "%s",
                            "original_price": %f,
                            "sale_price": %f,
                            "procity": "%s",
                            "real_sales": "%s",
                            "nick": "%s",
                            "detail_url": "%s",
                            "created_at":"%s",
                            "updated_at":"%s"
                        }
                        """,
                product.getId(),
                product.getTitle(),
                product.getPicPath(),
                product.getOriginalPrice(),
                product.getSalePrice(),
                product.getProcity(),
                product.getRealSales(),
                product.getNick(),
                product.getDetailUrl(),
                product.getCreatedAt(),
                product.getUpdatedAt()
        );
        return res;
    }

    public String genareteRelatedProductQuery(Post originalArticle) {
        String res = String.format("""
                {
                  "from": 0,
                  "size": 8,
                  "query": {
                    "bool": {
                      "should": [
                        {
                          "multi_match": {
                            "query": "%s",
                            "fields": ["title"]
                          }
                        },
                        {
                          "multi_match": {
                            "query": "%s",
                            "fields": ["title"]
                          }
                        }
                      ]
                    }
                  }
                }
                    """, originalArticle.getTitle(), originalArticle.getTags());
        return res;
    }
}